package com.example.propertymangesystem.controller;

import com.example.propertymangesystem.controller.util.R;
import com.example.propertymangesystem.domain.department;
import com.example.propertymangesystem.domain.manger;
import com.example.propertymangesystem.service.IDepartmentService;
import com.example.propertymangesystem.service.IMangerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/departments")
public class departmentController {
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping
    public R getAll(){
        return new R(true, departmentService.list(null));
    }

    @GetMapping("/{current}/{size}")
    public R getPage(@PathVariable Integer current, @PathVariable Integer size,department department){
        return new R(true, departmentService.getPage(current, size, department));
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id){
        department department = departmentService.getById(id);
        boolean b = department != null;
        return new R(b, department);
    }

    @PutMapping
    public R update(@RequestBody department department){
        boolean b = departmentService.updateById(department);
        return new R(b, b ? "修改成功^_^":"修改失败^…^");
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        boolean b = departmentService.removeById(id);
        return new R(b, b ? "删除成功^_^":"删除失败^…^");
    }

    @PostMapping
    public R save(@RequestBody department department){
        System.out.println(department);
        boolean b = departmentService.save(department);
        return new R(b, b ? "添加成功^_^":"添加失败^…^");
    }

}
